package com.edgclub.edg.uc;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.edgclub.edg.R;
import com.edgclub.edg.adapter.MenuPopAdapter;
import com.edgclub.edg.utils.util.UserPrefUtils;

import java.util.ArrayList;

/**
 *
 *
 */
public class MenuPopWindow implements AdapterView.OnItemClickListener {

    private Activity context;
    public static final String tag_look_owner = "tag_look_owner";
    public static final String tag_order = "tag_order";
    public static final String tag_share = "tag_share";
    public static final String tag_delete = "tag_delete";
    public static final String tag_add_to_digest = "tag_add_to_digest";
    public static final String tag_update = "tag_update";
    public static final String tag_report = "tag_report";
    public static final String tag_link = "tag_link";
    public static final String tag_add_channel = "tag_add_channel";
    public static final String tag_remove_channel = "tag_remove_channel";

    private MenuClickListener menuClickListener;

    private PopupWindow popupWindow;

    private GridView gridview;

    private MenuPopAdapter adapter;
    private ArrayList<MenuPopAdapter.MenuData> menuDatas;

    private static MenuPopWindow instance;
    private UserPrefUtils userPrefUtils;

    private int currPosition;

    private int popupHeight;

    public MenuPopWindow(Activity context, boolean isMyPost, boolean isAdddigest, boolean hasChannel) {
        this.context = context;
        menuDatas = new ArrayList<>();
        userPrefUtils = new UserPrefUtils(context);
        init(isMyPost, isAdddigest, hasChannel);
    }

    private void init(boolean isMyPost, boolean isAdddigest, boolean hasChannel) {
        View view = context.getLayoutInflater().inflate(R.layout.popwindow_topic_detail, null, false);
        popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        //必须设置背景
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //设置焦点
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);

        initdata(isMyPost, isAdddigest, hasChannel);
        gridview = (GridView) view.findViewById(R.id.gridview);
        adapter = new MenuPopAdapter(context, menuDatas);
        gridview.setAdapter(adapter);
        gridview.setOnItemClickListener(this);

        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        popupHeight = view.getMeasuredHeight();

    }

    private void initdata(boolean isMyPost, boolean isAdddigest, boolean hasChannel) {
        menuDatas.clear();
        menuDatas.add(new MenuPopAdapter.MenuData(R.drawable.menu_zhikanlouz, "只看楼主", tag_look_owner));
//		menuDatas.add(new MenuPopAdapter.MenuData(R.drawable.menu_order,"倒序查看",tag_order));
        menuDatas.add(new MenuPopAdapter.MenuData(R.drawable.menu_share, "分享", tag_share));
        if (isMyPost || userPrefUtils.isDeletTopic()) {
            menuDatas.add(new MenuPopAdapter.MenuData(R.drawable.menu_delete, "删除", tag_delete));
        }
        if (isMyPost) {
            menuDatas.add(new MenuPopAdapter.MenuData(R.drawable.menu_update, "修改", tag_update));
        }
        if (userPrefUtils.isRaiseTopic()) {
            if (isAdddigest) {
                menuDatas.add(new MenuPopAdapter.MenuData(R.drawable.menu_add_to_digest, "取消加精", tag_add_to_digest));
            } else {
                menuDatas.add(new MenuPopAdapter.MenuData(R.drawable.menu_add_to_digest, "加精", tag_add_to_digest));
            }
        }

        menuDatas.add(new MenuPopAdapter.MenuData(R.drawable.menu_report, "举报", tag_report));
        menuDatas.add(new MenuPopAdapter.MenuData(R.drawable.menu_link, "复制链接", tag_link));

        if (userPrefUtils.isMoveTopic()){
            if (hasChannel){
                menuDatas.add(new MenuPopAdapter.MenuData(R.drawable.menu_remove_channel, "移出频道", tag_remove_channel));
            } else {
                menuDatas.add(new MenuPopAdapter.MenuData(R.drawable.menu_add_channel, "添加频道", tag_add_channel));
            }
        }

//		adapter.setMenuDatas(menuDatas);
//		adapter.notifyDataSetChanged();
    }

    public static MenuPopWindow getInstance(Activity context, boolean isMyPost, boolean isAdddigest, boolean hasChannel) {
        if (instance == null) {
            instance = new MenuPopWindow(context, isMyPost, isAdddigest, hasChannel);
        }
        return instance;
    }

    public void setInstance(MenuPopWindow instance) {
        MenuPopWindow.instance = instance;
    }

    public void showPopWindow(View view) {
//		initdata(isMyPost,isAdddigest);
//		popupWindow.showAsDropDown(view);
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, location[0], location[1] - popupHeight);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        currPosition = i;
        MenuPopAdapter.MenuData menuData = menuDatas.get(i);
        if (menuClickListener == null) return;
        if (menuData.tag.equals(tag_look_owner)) {
            menuClickListener.onMenuClick(tag_look_owner);
            if (menuData.name.contains("全部")) {
                menuData.name = "只看楼主";
            } else {
                menuData.name = "查看全部";
            }

        } else if (menuData.tag.equals(tag_order)) {
            if (menuData.name.contains("倒序")) {
                menuData.name = "正序查看";
                menuData.icon = R.drawable.menu_order2;
            } else {
                menuData.name = "倒序查看";
                menuData.icon = R.drawable.menu_order;
            }
            menuClickListener.onMenuClick(tag_order);
        } else if (menuData.tag.equals(tag_share)) {
            menuClickListener.onMenuClick(tag_share);
        } else if (menuData.tag.equals(tag_delete)) {
            menuClickListener.onMenuClick(tag_delete);
        } else if (menuData.tag.equals(tag_add_to_digest)) {
//			if(menuData.name.contains("取消加精")){
//				menuData.name = "加精";
//			}else{
//				menuData.name = "取消加精";
//			}
            menuClickListener.onMenuClick(tag_add_to_digest);
        } else if (menuData.tag.equals(tag_update)) {
            menuClickListener.onMenuClick(tag_update);
        } else if (menuData.tag.equals(tag_report)) {
            menuClickListener.onMenuClick(tag_report);
        } else if (menuData.tag.equals(tag_link)) {
            menuClickListener.onMenuClick(tag_link);
        }else if (menuData.tag.equals(tag_add_channel)) {
            menuClickListener.onMenuClick(tag_add_channel);
        }else if (menuData.tag.equals(tag_remove_channel)) {
            menuClickListener.onMenuClick(tag_remove_channel);
        }

        popupWindow.dismiss();
        adapter.notifyDataSetChanged();

    }

    public void setisDigest(boolean isAdddigest) {
        MenuPopAdapter.MenuData menuData = menuDatas.get(currPosition);
        if (!isAdddigest) {
            menuData.name = "加精";
        } else {
            menuData.name = "取消加精";
        }
    }

    public interface MenuClickListener {
        public void onMenuClick(String type);

    }

    public MenuClickListener getMenuClickListener() {
        return menuClickListener;
    }

    public void setMenuClickListener(MenuClickListener menuClickListener) {
        this.menuClickListener = menuClickListener;
    }
}
